package com.hyfrogx.modules.manage.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.manage.entity.HyChannelApkEntity;
import com.hyfrogx.modules.manage.form.channel.ChannelForm;
import com.hyfrogx.modules.manage.form.channel.ChannelVo;
import com.hyfrogx.modules.manage.service.HyChannelApkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/9/24 16:29
 * @Company 汉咏科技
 * 参数渠道控制类
 */
@RestController
@RequestMapping("/channel/apk")
@Api(value = "HyChannelApkController", tags = {"后台--渠道管理"})
public class HyChannelApkController {
    @Autowired
    private HyChannelApkService hyChannelApkService;


    /**
     * 按条件分页查询渠道列表
     */
    @PostMapping("/get/channel")
    @ApiOperation("按条件分页查询渠道列表")
    public R getChannel(@RequestBody Map<String,Object> map)
    {

        IPage<ChannelVo> page = hyChannelApkService.getChannelPage(map);
        return R.ok().put("page",page);
    }
    @PostMapping("/get/list")
    @ApiOperation("查询渠道列表")
    public R getList()
    {
        List<HyChannelApkEntity> list = hyChannelApkService.list(new QueryWrapper<HyChannelApkEntity>().eq("status",1));
        return R.ok().put("list",list);
    }

    /**
     * 新增渠道信息
     */
    @PostMapping("/insert/channel")
    @ApiOperation("新增渠道信息")
    public R insertChannel(@RequestBody @Validated ChannelForm channelForm)
    {
        //判断渠道号或渠道名是否存在
        List<HyChannelApkEntity> list = hyChannelApkService.list((new QueryWrapper<HyChannelApkEntity>().eq("channelNum",channelForm.getChannelNum())
                                                        .or().eq("channelName",channelForm.getChannelName())));
        if (list.size()>0)
        {
            return R.error("渠道号或渠道名称重复");
        }
        //如果不存在，新建
        Boolean b = hyChannelApkService.insertChannel(channelForm);
        if (b)
        {
            return R.ok();
        }
        else
        {
            return R.error("创建失败");
        }
    }

    /**
     * 编辑渠道信息
     */
    @PostMapping("/update/channel")
    @ApiOperation("编辑渠道信息")
    public R updateChannel(@RequestBody @Validated ChannelForm channelForm)
    {
        //判断渠道是否存在
        HyChannelApkEntity hyChannelApkEntity = hyChannelApkService.getById(channelForm.getId());
        if (ObjectUtil.isNull(hyChannelApkEntity))
        {
            return R.error("该渠道不存在");
        }
       //如果渠道存在，判断除了该渠道外，渠道号或渠道名称是否重复
        List<HyChannelApkEntity> list = hyChannelApkService.list((new QueryWrapper<HyChannelApkEntity>().eq("channelNum",channelForm.getChannelNum())
                .or().eq("channelName",channelForm.getChannelName()).ne("id",channelForm.getId())));
        if (list.size()>0)
        {
            return R.error("渠道号或渠道名称重复");
        }
        //如果渠道号不重复，修改
        Boolean b = hyChannelApkService.updateChannel(channelForm);
        if (b)
        {
            return R.ok();
        }
        else
        {
            return R.error("修改失败");
        }
    }

    /**
     * 修改渠道状态
     */
    @PostMapping("/update/status/{id}")
    @ApiOperation("修改渠道状态")
    public R updateStatus(@PathVariable("id") Integer id)
    {
        if (hyChannelApkService.updateStatus(id))
        {
            return R.ok();
        }
        else {
            return R.error("更新失败");
        }

    }

    /**
     * 删除渠道
     * @param id
     * @return
     */
    @PostMapping("/delete/channel/{id}")
    @ApiOperation("删除渠道")
    public R deleteChannel(@PathVariable("id") Integer id)
    {
        if (hyChannelApkService.deleteChannel(id))
        {
            return  R.ok();
        }
        else {
            return R.error("删除失败");
        }
    }



}
